package model01;

import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class WriteBinaryToFile {
	public static void main(String[] args) {
		String strVal = null;
		int iVal = 0;
		float fVal = 0.0f;
		double dVal = 0.0;
		
		DataOutputStream dos;
		FileOutputStream fos;
		Scanner s;
		
		try {
			//mo file binaryfile.dat de ghi, neu khong co se tao moi
			fos = new FileOutputStream("binaryfile.dat");
			//tao luong DataOutputStream danh cho viec ghi file, dos se lam viec truc tiep voi file
			dos = new DataOutputStream(fos);
			s = new Scanner(System.in);
			
			System.out.print("Kieu String: ");
			strVal = s.nextLine();
			
			System.out.print("Kieu int: ");
			iVal = s.nextInt();
			
			System.out.print("Kieu float: ");
			fVal = s.nextFloat();
			
			System.out.print("Kieu double: ");
			dVal = s.nextDouble();
			
			//ghi ra file, phai ghi theo thu tu nhu da khai bao
			try {
				dos.writeUTF(strVal);
				dos.writeInt(iVal);
				dos.writeFloat(fVal);
				dos.writeDouble(dVal);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			//dong stream
			dos.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
